<?php
namespace Home\Controller;

use Util\AccessController;

class LoginController extends AccessController {

    public function test() {
        $this->S('_REMOVE_');
        $where['openid'] = 'oib6I0f22PsSYtn1Ih1KstspE8_k';
        $ls = Service('User', 'u_user');
        $model = $ls->find($where);
        $this->S($model);
        dump($this->S());
    }

    public function outCache() {
        $this->S('_REMOVE_');
        echo '清缓存成功';
    }

    public function index() {
        $URL = 'http://' . $_SERVER['HTTP_HOST'];
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $wechat->getCode($URL . "/BookS/index.php/Login/Handle?PrevURL={$_GET['PrevURL']}", false);
    }

    public function Handle() {
//        $URL = 'http://' . $_SERVER['HTTP_HOST'];
        $PrevURL = $_GET['PrevURL'];
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $userInfo = $wechat->WechatLogin();
        $this->S($userInfo);
        redirect($PrevURL);

//        if ($userInfo && $userInfo['openid'] != 'C') {
//            $where['user_wx'] = $userInfo['openid'];
//            $ls = Service('User', 'u_user');
//            $check = $ls->find($where);
//            if (!$check) {
//                /*初次登录用户保存信息*/
//                $model['user_id'] = 'WX_' . uniqid() . '_' . rand(0, 9) . rand(10, 99);
//                $model['user_wx'] = $userInfo['openid'];
//                $model['user_wx_name'] = $userInfo['nickname'];
//                $model['user_image'] = $userInfo['headimgurl'];
//
//                $model['user_area_sheng'] = $userInfo['province'];
//                $model['user_area_shi'] = $userInfo['city'];
//                $model['sex'] = $userInfo['sex'];
//                $model['user_time'] = date("Y-m-d H:i:s");
//                $model['login_time'] = date("Y-m-d H:i:s");
//                if ($userInfo['openid']) {
//                    $inserCheck = $ls->insert($model);
//                    $data1 = $ls->find($model);
//                    if ($data1 && $inserCheck) {
//                        $res = $data1;
//                    } else {
//                        $this->error('微信登录失败！正在重试！' . $userInfo['openid'], 'index');
//                    }
//                } else {
//                    $this->error('微信登录失败！正在重试！' . $userInfo['openid'], 'index');
//                }
//            } else {
//                /*老用户*/
//                $updateSave['login_time'] = date("Y-m-d H:i:s");
//                $updateSave['user_wx_name'] = $userInfo['nickname'];
//                $updateSave['user_image'] = $userInfo['headimgurl'];
//                $updateSave['user_area_sheng'] = $userInfo['province'];
//                $updateSave['user_area_shi'] = $userInfo['city'];
//                $res = $check;
//                $ls->update($where, $updateSave);
//            }
//            //资金表是否存在，不存在添加
//            $CLS = Service('M', 'cost');
//            $cwhere['USER_ID'] = $res['user_id'];
//            $cdata = $CLS->find($cwhere);
//            if (!$cdata) {
//                $csave['USER_ID'] = $res['user_id'];
//                $csave['MONEY'] = passport_encrypt('0.0', C('JM_KEY'));
//                $csave['BOOK_B'] = 0;
//                $csave['UPDATE_TIME'] = date('Y-m-d H:i:s');
//                $csave['UPDATE_IP'] = get_client_ip();
//                $C_A_OK = $CLS->Add($csave);
//            } else {
//                $C_A_OK = 1;
//            }
//
//            if ($C_A_OK) {
//                $str = '?token=' . passport_encrypt($res['user_id'], C('JM_KEY'));
//                redirect($PrevURL . $str);
//            } else {
//                $this->error('添加资金表失败！' . $userInfo['openid'], 'index');
//            }
//
//        } else {
//            redirect('Index');
//        }

    }


}